package com.atchangsha.system.controller;

import java.util.Arrays;
import java.util.Map;

import com.atchangsha.system.constant.MenusOrderRelationConstants;
import com.atchangsha.system.dto.MenusOrderRelationDTO;
import org.springframework.web.bind.annotation.*;
import com.atchangsha.system.entity.MenusOrderRelationEntity;
import com.atchangsha.system.service.MenusOrderRelationService;
import com.atchangsha.system.common.utils.PageUtils;
import com.atchangsha.system.common.utils.Result;
import javax.annotation.Resource;



/**
 * 菜单订单关系表
 * @author 卓越班项目第二组
 * @date 2022-04-22 00:49:58
 */
@RestController
@RequestMapping("system/menusOrderRelation")
@CrossOrigin
public class MenusOrderRelationController {
    @Resource
    private MenusOrderRelationService menusOrderRelationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = menusOrderRelationService.queryPage(params);
        return Result.ok().setResult(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		MenusOrderRelationEntity menusOrderRelation = menusOrderRelationService.getById(id);
        //判断所查询是否为空
        if (menusOrderRelation == null){
            return Result.fail(MenusOrderRelationConstants.MenusOrderRelationEnum.MenusOrderRelation_INFO_FILE);
        }
        return Result.ok().setResult(menusOrderRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody MenusOrderRelationDTO menusOrderRelation){
        //判断所插入的数据是否存在
        MenusOrderRelationEntity menusOrderRelationEntity = menusOrderRelation.toEntity();
        MenusOrderRelationEntity menusOrderRelationExit = menusOrderRelationService.getById(menusOrderRelationEntity.getId());
        if (menusOrderRelationExit != null){
            return Result.fail(MenusOrderRelationConstants.MenusOrderRelationEnum.MenusOrderRelation_SAVE_FILE);
        }
		menusOrderRelationService.save(menusOrderRelationEntity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody MenusOrderRelationDTO menusOrderRelation){
        MenusOrderRelationEntity menusOrderRelationEntity = menusOrderRelation.toEntity();
        menusOrderRelationService.updateById(menusOrderRelationEntity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long[] ids){
		menusOrderRelationService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
